package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.cargo.ContractProductVo;
import cn.itcast.service.cargo.ContractService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ContractProductDao contractProductDao;

    //保存购销合同
    public void save(Contract contract) {
        //1.设置唯一id
        contract.setId(UUID.randomUUID().toString());
        //2.设置基本数据
        contract.setState(0); //设置状态  0：草稿 1：已确认
        contract.setCreateTime(new Date()); //创建时间
        contract.setProNum(0); //货物数量
        contract.setExtNum(0); //附件数量
        contract.setTotalAmount(0d);
        contractDao.insertSelective(contract);

    }
    //删除
    public void delete(String id) {
        //删除附件（根据购销合同id删除附件）
        extCproductDao.deleteByContractId(id);
        //删除货物(根据购销合同id删除货物)
        contractProductDao.deleteByContractId(id);
        //删除购销合同
        contractDao.deleteByPrimaryKey(id);

    }
    //更新
    public void update(Contract contract) {
          contractDao.updateByPrimaryKeySelective(contract);
    }
    //根据id查询
    public Contract findById(String id) {
        return contractDao.selectByPrimaryKey(id);
    }
    //查询分页
    public PageInfo findAll(ContractExample example, int page, int size) {
        //设置分页参数
        PageHelper.startPage(page,size);
        //查询全部
        List<Contract> list = contractDao.selectByExample(example);
        //构造结果
        return new PageInfo(list);
    }

   //根据船期构造出货表数据
    public List<ContractProductVo> findByShipTime(String inputDate) {
        return contractDao.findByShipTime(inputDate);
    }
}
